package com.hx.voice.recognition.auth.server.runner;

import com.hx.voice.recognition.auth.server.configuration.KeyConfiguration;
import com.hx.zjb.security.auth.common.util.jwt.RsaKeyHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

/**
 * @descripe：程序一启动，执行生成 用户公钥 用户私钥 应用公钥 应用私钥,没有配置configuration，所有没有让spring扫描到
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/14 10:56
 * @version：V1.0
 */
@Slf4j
@Configuration
public class AuthServerRunner implements CommandLineRunner {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private KeyConfiguration keyConfiguration;

    private static final String REDIS_USER_PRI_KEY = "CRM-AUTH:JWT:PRI";
    private static final String REDIS_USER_PUB_KEY = "CRM-AUTH:JWT:PUB";
    private static final String REDIS_SERVICE_PRI_KEY = "CRM-AUTH:CLIENT:PRI";
    private static final String REDIS_SERVICE_PUB_KEY = "CRM-AUTH:CLIENT:PUB";

    @Override
    public void run(String... args) throws Exception {
        log.info("======================================授权服务端启动，开始生成应用以及用户公钥私钥，用于jwt加密解密等============================================");
        //生成用户公钥 私钥 应用公钥 私钥
        if(redisTemplate.hasKey(REDIS_USER_PRI_KEY) && redisTemplate.hasKey(REDIS_USER_PUB_KEY) && redisTemplate.hasKey(REDIS_SERVICE_PRI_KEY) && redisTemplate.hasKey(REDIS_SERVICE_PUB_KEY))
        {
            keyConfiguration.setUserPriKey(RsaKeyHelper.toBytes(redisTemplate.opsForValue().get(REDIS_USER_PRI_KEY)));
            keyConfiguration.setUserPubKey(RsaKeyHelper.toBytes(redisTemplate.opsForValue().get(REDIS_USER_PUB_KEY)));
            keyConfiguration.setServicePriKey(RsaKeyHelper.toBytes(redisTemplate.opsForValue().get(REDIS_SERVICE_PRI_KEY)));
            keyConfiguration.setServicePubKey(RsaKeyHelper.toBytes(redisTemplate.opsForValue().get(REDIS_SERVICE_PUB_KEY)));
        }
        else
        {
            //生成用户公钥 私钥
            Map<String, byte[]> stringMap = RsaKeyHelper.generateKey(keyConfiguration.getUserSecret());
            keyConfiguration.setUserPriKey(stringMap.get("pri"));
            keyConfiguration.setUserPubKey(stringMap.get("pub"));
            redisTemplate.opsForValue().set(REDIS_USER_PRI_KEY, RsaKeyHelper.toHexString(keyConfiguration.getUserPriKey()));
            redisTemplate.opsForValue().set(REDIS_USER_PUB_KEY, RsaKeyHelper.toHexString(keyConfiguration.getUserPubKey()));

            stringMap.clear();

            //生成应用公钥私钥
            stringMap = RsaKeyHelper.generateKey(keyConfiguration.getServiceSecret());
            keyConfiguration.setServicePriKey(stringMap.get("pri"));
            keyConfiguration.setServicePubKey(stringMap.get("pub"));
            redisTemplate.opsForValue().set(REDIS_SERVICE_PRI_KEY, RsaKeyHelper.toHexString(keyConfiguration.getServicePriKey()));
            redisTemplate.opsForValue().set(REDIS_SERVICE_PUB_KEY, RsaKeyHelper.toHexString(keyConfiguration.getServicePubKey()));
        }
    }
}